package com.freecloud.javabasics.network.socket.demo2;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * BIO模型
 * @Author: maomao
 * @Date: 2020-03-15 12:44
 */
public class ServerSocketDemo2 {

    static ExecutorService executorService = Executors.newCachedThreadPool();

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            //服务端一定需要监听一个端口号，Ip默认使用本机IP地址
            serverSocket = new ServerSocket(8080);
            while (true){
                //接收客户端的连接
                Socket socket = serverSocket.accept();
                //通过线程的方式不让连接阻塞(线程不可控)
                //new Thread(new com.freecloud.javabasics.network.socket.demo2.ServerSocket(socket)).start();

                //使用线程池
                executorService.execute(new com.freecloud.javabasics.network.socket.demo2.ServerSocket(socket));
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
